package com.easylinkin.linkappapi.common.service;

import cn.hutool.core.util.ObjectUtil;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.ObjectMetadata;
import com.aliyun.oss.model.PutObjectRequest;
import com.easylinkin.linkappapi.facerecognition.service.oss.ObjectStorageOutsideService;
import com.easylinkin.linkappapi.personality.constant.PersonalityConstant;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import sun.misc.BASE64Decoder;

@Service
public class OSSServiceImpl implements OSSService {

	@Value("${oss.dir}")
	private String dir;
	@Value("${oss.bucket}")
	private String bucket;
	@Value("${oss.uploadHost}")
	private String host;

	@Value("${oss.readHost}")
	private String readHost;

	@Value("${oss.type}")
	private String ossType;

	@Value("${minio.uploadHost:/api/oss/download}")
	private String accessHost;

	@Autowired
	@Qualifier("oss")
	private ObjectStorageOutsideService ossClient;

	@Override
	public String saveImgBase64(String fileStr, String fileName) {
		if (ObjectUtil.isNotEmpty(fileStr)){
			String result = "";
			// 获取 base64
			String data = fileStr.substring(fileStr.indexOf(",") + 1); // 去除前缀
			BASE64Decoder decoder = new BASE64Decoder();

			try {
				byte[] b = decoder.decodeBuffer(data);
				for ( int i=0; i<b.length; ++i){
					if (b[i]<0){ //调整异常数据
						b[i]+=256;
					}
				}
				InputStream sbs = new ByteArrayInputStream(b);

				String name = "tmp";
				if (ObjectUtil.isNotEmpty(fileName) && fileName.contains("."))
					name = fileName.substring(0,fileName.indexOf("."));
				String s = uploadImg(sbs, name,null);
				return s;

			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		return null;
	}

	@Override
	public String uploadImg(InputStream ico, String fileName,String suffix) throws IOException {
		if (StringUtils.isBlank(suffix)){
			suffix = "png";
		}

		PersonalityConstant.FileTypeEnum fileTypeEnum = PersonalityConstant.FileTypeEnum.valueOf(1); // 默认图片
		String path = dir+ (fileTypeEnum==null?"default/":fileTypeEnum.getPath()) + "jiangong/";

		SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmsss");

		String uploadName = path + format.format(new Date()) + "-" + fileName + "."+suffix;

		ObjectMetadata metadata = new ObjectMetadata();
		metadata.setContentType("image/"+suffix);

//		OSS ossClient = new OSSClient(endPoint,accessId,accessSecret);
//
//		PutObjectRequest putObjectRequest = new PutObjectRequest(bucket, uploadName,ico);
//		ossClient.putObject(putObjectRequest);
//		ossClient.shutdown();
		ossClient.uploadImgObject(bucket,uploadName,ico);
		String uploadUrl = generateUrl(uploadName);
		ico.close();
		return uploadUrl;

	}

	@Override
	public InputStream down(String fileName) {
		InputStream inputStream = ossClient.getObjectInputStream(bucket, fileName);
		return inputStream;
	}

	@Override
	public String generateUrl(String fileName) {
		String uploadUrl = null;
		if("minio".equalsIgnoreCase(ossType)){
			uploadUrl = readHost+"/"+bucket+"/"+fileName;
		}else {
			uploadUrl = host+"/"+fileName;
		}
		return uploadUrl;
	}
}
